package com.apiserver.data.mybatis.reflet;


import com.apiserver.data.annotation.Column;
import com.apiserver.data.annotation.Id;
import com.apiserver.data.annotation.IdStrategy;
import com.apiserver.data.annotation.Style;
import com.apiserver.data.mybatis.model.ColumnProperty;
import com.apiserver.data.mybatis.util.StyleUtil;

import javax.persistence.Entity;
import javax.persistence.Transient;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author feizhou
 * @Description ColumnProperty工具类
 * @Date 下午 2:25 2019/10/9 0009
 * @Param
 * @return
 **/

public class ColumnPropertyUtil {

    private static final IColumnPropertyHelper columnPropertyHelper;

    private static final String JAVA_VERSION = System
            .getProperty("java.version");

    static {
        if (JAVA_VERSION.contains("1.8.")) {
            columnPropertyHelper = new Jdk8ColumnPropertyHelper();
        } else {
            //目前先不处理
            columnPropertyHelper = new Jdk8ColumnPropertyHelper();
        }
    }

    /**
     * 获取ColumnProperty
     *
     * @param entityClass
     * @return
     */
    public static List<ColumnProperty> getColumnProperties(Class<?> entityClass, Style style) {
        return columnPropertyHelper.getColumnProperties(entityClass, style);
    }


    /**
     * Field接口
     */
    interface IColumnPropertyHelper {

        /**
         * 获取ColumnPropertie
         *
         * @param entityClass
         * @return
         */
        List<ColumnProperty> getColumnProperties(Class<?> entityClass, Style style);


    }

    /**
     * 支持jdk8
     */
    static class Jdk8ColumnPropertyHelper implements IColumnPropertyHelper {

        /**
         * 获取全部的getColumnProperties
         *
         * @param entityClass
         * @return
         */
        public List<ColumnProperty> getColumnProperties(Class<?> entityClass, Style style) {

            return _getColumnProperties(entityClass, null, null, style);
        }

        /**
         * @return java.util.List<com.apiserver.data.mybatis.model.ColumnProperty>
         * @Author feizhou
         * @Description 获取全部的ColumnPropertie
         * @Date 下午 1:41 2019/10/9 0009
         * @Param [entityClass, list, level]
         **/

        private List<ColumnProperty> _getColumnProperties(Class<?> entityClass, List<ColumnProperty> list, Integer level, Style style) {

            if (list == null) {
                list = new ArrayList<ColumnProperty>();
            }
            if (level == null) {
                level = 0;
            }
            //Object就直接返回
            if (entityClass.equals(Object.class)) {
                return list;
            }

            //获取所有的字段
            Field[] fields = entityClass.getDeclaredFields();
            int index = 0;

            for (int i = 0; i < fields.length; i++) {

                Field field = fields[i];
                //排除构造参数this
                if (field.getName().contains("this$0")) {
                    continue;
                }

                // 排除静态字段
                if (!Modifier.isStatic(field.getModifiers())) {

                    ColumnProperty columnProperty = getColumnProperty(field, style);

                    if (level.intValue() != 0) {
                        // 将父类的字段放在前面
                        list.add(index, columnProperty);
                        index++;
                    } else {
                        list.add(columnProperty);
                    }
                }
            }


            //构建父类的字段,迭代
            Class<?> superClass = entityClass.getSuperclass();
            if (superClass != null && !superClass.equals(Object.class)
                    && (superClass.isAnnotationPresent(Entity.class)
                    || (!Map.class.isAssignableFrom(superClass) && !Collection.class.isAssignableFrom(superClass)))) {

                return _getColumnProperties(entityClass.getSuperclass(), list, ++level, style);

            }
            return list;
        }

        /**
         * @return void
         * @Author feizhou
         * @Description 获取ColumnProperty
         * @Date 下午 1:38 2019/10/9 0009
         * @Param [field]
         **/

        private ColumnProperty getColumnProperty(Field field, Style style) {

            String propertyName = field.getName();
            //数据库列名
            String columnName = propertyName;

            Class<?> javaType = field.getType();
            Boolean isPK = false;
            Boolean isTransient = false;
            IdStrategy idStrategy=null;

            //  Transient注解
            if (field.isAnnotationPresent(Transient.class)) {
                isTransient = true;
            }

            // 主键
            if (field.isAnnotationPresent(Id.class)) {
                isPK = true;
                idStrategy=field.getAnnotation(Id.class).value();
            }

            //数据库列名
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getAnnotation(Column.class);
                columnName = column.name();
            } else {
                columnName = StyleUtil.convertByStyle(columnName, style);
            }


            return new ColumnProperty(columnName, propertyName, isPK, isTransient, null, javaType,idStrategy);
        }

    }


}
